package com.tulele.vtool.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 获取当前时间
     * @return
     */
    public static Date getCurDate() {
        return new Date();
    }

    /**
     * 日期格式化
     */
    public static String getFormatDate(String format, Date date) {

        DateTime dateTime = DateTime.now();
        if (date != null) {
            dateTime = new DateTime(date);
        }

        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        return dateTime.toString(format);
    }

    /**
     * 日期格式化
     */
    public static String getFormatDate(String format, DateTime date) {

        DateTime dateTime = date;
        if (date == null) {
            dateTime = DateTime.now();
        }

        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss.SSS";
        }

        return dateTime.toString(format);
    }

    /**
     * 获取指定日期的周一
     * @param date
     * @return
     */
    public static String getFirstDayOfWeek(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime firstDayTime = dateTime.withDayOfWeek(1);

        return firstDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取指定日期的周日
     * @param date
     * @return
     */
    public static String getLastDayOfWeek(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime lastDayTime = dateTime.withDayOfWeek(7);

        return lastDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取指定日期的所属月份第一天
     * @param date
     * @return
     */
    public static String getFirstDayOfMonth(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime firstDayTime = dateTime.withDayOfMonth(1);

        return firstDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取指定日期的所属月份最后一天
     * @param date
     * @return
     */
    public static String getLastDayOfMoth(Date date) {
        DateTime dateTime = new DateTime(date);

        DateTime lastDayTime = dateTime.dayOfMonth().withMaximumValue();

        return lastDayTime.toString("YYYY-MM-dd");
    }

    /**
     * 获取本季的第一天
     * @return
     */
    public static String getFirstDayOfSeason() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.MONTH, getFirstMonthOfSeason(calendar) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取本季的最后一天
     * @return
     */
    public static String getLastDayOfSeason() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.MONTH, getFirstMonthOfSeason(calendar) + 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取本季的第一个月
     * @param calendar
     * @return
     */
    public static Integer getFirstMonthOfSeason(Calendar calendar) {
        Integer result = 0;
        Integer currentMonth = calendar.get(Calendar.MONTH) + 1;
        switch (currentMonth) {
            case 1:
            case 2:
            case 3:
                result = 1;
                break;
            case 4:
            case 5:
            case 6:
                result = 4;
                break;
            case 7:
            case 8:
            case 9:
                result = 7;
                break;
            default:
                result = 10;
                break;
        }
        return result;
    }

    /**
     * 获取当前季度开始月份
     * @return
     */
    public static int getFirstMonthOfSeason() {
        int firstMonth = 0;
        Calendar calendar = Calendar.getInstance();
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        switch (currentMonth) {
            case 1:
            case 2:
            case 3:
                firstMonth = 1;
                break;
            case 4:
            case 5:
            case 6:
                firstMonth = 4;
                break;
            case 7:
            case 8:
            case 9:
                firstMonth = 7;
                break;
            default:
                firstMonth = 10;
                break;
        }
        return firstMonth;
    }

    /**
     * 获取当前季度结束月份
     * @return
     */
    public static int getLastMonthOfSeason() {
        int lastMonth = 0;
        Calendar calendar = Calendar.getInstance();
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        switch (currentMonth) {
            case 1:
            case 2:
            case 3:
                lastMonth = 3;
                break;
            case 4:
            case 5:
            case 6:
                lastMonth = 6;
                break;
            case 7:
            case 8:
            case 9:
                lastMonth = 9;
                break;
            default:
                lastMonth = 12;
                break;
        }
        return lastMonth;
    }

    /**
     * 获取VIP结束时间
     * @param date
     * @param vipLev
     * @return
     */
    public static Date getVipEndTime(Date date, int vipLev) {

        DateTime startTime = new DateTime(date);

        DateTime endTime = new DateTime();

        if (1 == vipLev) {

            endTime = startTime.plusMonths(6);

        } else if (2 == vipLev) {

            endTime = startTime.plusYears(1);

        } else if (3 == vipLev) {

            endTime = startTime.plusYears(3);

        }

        return endTime.toDate();
    }

    public static String getWeek(int order) {
        String week = null;

        switch (order) {
            case 0:
                week = SysConstant.MONDAY;
                break;
            case 1:
                week = SysConstant.TUESDAY;
                break;
            case 2:
                week = SysConstant.WEDNESDAY;
                break;
            case 3:
                week = SysConstant.THURSDAY;
                break;
            case 4:
                week = SysConstant.FRIDAY;
                break;
            case 5:
                week = SysConstant.SATURDAY;
                break;
            case 6:
                week = SysConstant.SUNDAY;
                break;
            default:
                week = "";
                break;
        }

        return week;
    }

    /**
     * 获取VIP有效期的天数
     * @param date
     * @return
     */
    public static int getVipValidDays(Date date) {

        DateTime startTime = DateTime.now();
        DateTime endTime = new DateTime(date);

        Period period = new Period(startTime, endTime, PeriodType.days());

        int validDays = period.getDays();

        return validDays >= 0 ? validDays : 0;
    }

    /**
     * 计算指定时间与当前时间的时间差
     * @param timeUnit 1-秒钟 2-分钟 3-小时
     * @param time
     * @return
     */
    public static int getTimeDiff(int timeUnit, String time) {

        DateTime spcialTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime(time);

        DateTime nowTime = DateTime.now();

        if (timeUnit == 1) {

            Period period = new Period(spcialTime, nowTime, PeriodType.seconds());

            return period.getSeconds();

        } else if (timeUnit == 2) {

            Period period = new Period(spcialTime, nowTime, PeriodType.minutes());

            return period.getMinutes();

        } else if (timeUnit == 3) {

            Period period = new Period(spcialTime, nowTime, PeriodType.hours());

            return period.getHours();

        }

        return 0;
    }

    /**
     * 计算指定时间与当前时间的时间差
     * @param timeUnit 1-秒钟 2-分钟 3-小时
     * @param time
     * @return
     */
    public static int getTimeDiff(int timeUnit, Date time) {

        DateTime spcialTime = new DateTime(time);

        DateTime nowTime = DateTime.now();

        if (timeUnit == 1) {

            Period period = new Period(spcialTime, nowTime, PeriodType.seconds());

            return period.getSeconds();

        } else if (timeUnit == 2) {

            Period period = new Period(spcialTime, nowTime, PeriodType.minutes());

            return period.getMinutes();

        } else if (timeUnit == 3) {

            Period period = new Period(spcialTime, nowTime, PeriodType.hours());

            return period.getHours();

        }

        return 0;
    }

    /**
     * 计算指定时间与当前时间的时间差
     * @param timeUnit 4-天 5-月 6-年
     * @param time
     * @return
     */
    public static int getTimeDifference(int timeUnit, Date time) {

        DateTime spcialTime = new DateTime(time);

        DateTime nowTime = DateTime.now();

        if (timeUnit == 4) {

            Period period = new Period(spcialTime, nowTime, PeriodType.days());

            return period.getDays();

        } else if (timeUnit == 5) {

            Period period = new Period(spcialTime, nowTime, PeriodType.months());

            return period.getMonths();

        } else if (timeUnit == 6) {

            Period period = new Period(spcialTime, nowTime, PeriodType.years());

            return period.getYears();

        }

        return 0;
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate 较大的时间
     * @return 相差天数
     */
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @return
     */
    public static Date timeStampToDate(String seconds) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return null;
        }
        //if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";
        //SimpleDateFormat sdf = new SimpleDateFormat(format);
        return new Date(Long.valueOf(seconds + "000"));
    }

        /**
         * 判断字符串能不能转 Double
         * @param str
         * @return
         */
    public static boolean isNumDouble(String str) {
        boolean ret = false;
        try {
            Double.parseDouble(str);
            ret = true;
        } catch (Exception ex) {
            ret = false;
        }
        return ret;
    }

    public static void main(String[] args) {
//        DateTime dateTime = DateTime.now();
//        DateTime firstDayTime = dateTime.withDayOfMonth(1);
//        DateTime lastDayTime = dateTime.dayOfMonth().withMaximumValue();
//        System.out.println(firstDayTime.toString("YYYY-MM-dd"));
//        System.out.println(lastDayTime.toString("YYYY-MM-dd"));

//        System.out.println(getVipValidDays(new DateTime(2020, 2, 1, 11, 11, 11, 11).toDate()));

//        System.out.println(getTimeDiff(2, "2019-03-21 13:13:20"));

        int hourDiff = DateUtil.getTimeDiff(3, "2019-04-23 21:34:33");
        System.out.println(hourDiff);
    }
}
